/*
 * @Author: your name
 * @Date: 2021-02-28 13:41:55
 * @LastEditTime: 2021-04-28 08:40:43
 * @LastEditors: Please set LastEditors
 * @Description: 子目录管理
 * @FilePath: \jianfa-api\service\menuService.js
 */
const {
    addMenuDao,
    addMenuFileDao,
    getMenuDao,
    getMenuFileDao,
    delMenuFileDao,
    delMenuDao,
    getMenuTowDao,
    getMenuFileNoPageDao,
    updateMenuDao,
} = require("../dao/menuDao");
const { delImg } = require("../utils/utils");
module.exports = {
    //修改三级菜单sort
    async updateMenuService(params) {
        return await updateMenuDao(params);
    },
    //添加菜单
    async addMenuService(params) {
        return await addMenuDao(params);
    },
    //添加菜单文件
    async addMenuFileService(filesInfo, sid, table_name) {
        return await addMenuFileDao(filesInfo, sid, table_name);
    },
    //获取三级菜单列表
    async getMenuService(table_name, cid) {
        const res = await getMenuDao(table_name, cid);
        res.results.sort((a, b) => b.sort - a.sort);
        return res;
    },
    //获取三级菜单列表
    async getMenuTowService(table_name) {
        const res = await getMenuTowDao(table_name);
        res.results.sort((a, b) => b.sort - a.sort);
        return res;
    },
    //获取三级菜单文件数据
    async getMenuFileService(params) {
        return await getMenuFileDao(params);
    },
    //获取三级菜单文件数据不分页
    async getMenuFileNoPageService(params) {
        return await getMenuFileNoPageDao(params);
    },
    //更具id删除三级菜单文件数据
    async delMenuFileService({ table_name, id }) {
        const { results } = await module.exports.getMenuFileService({ table_name, id }); //查询文件路径
        const { status } = await delMenuFileDao({ table_name, id }); //删除数据库文件
        if (status) {
            return await delImg(results[0]["file_path"]); //删除本地图片
        }
    },
    //更具id删除三级菜单
    async delMenuService({ table_name, id }) {
        const { results } = await module.exports.getMenuFileNoPageService({ table_name: table_name + "_files", sid: id }); //查询文件路径
        const { status } = await delMenuDao({ table_name, id }); //删除数据库文件
        console.log("更具id删除三级菜单文件数据 results :>> ", results);
        if (status) {
            results.forEach(async (item) => {
                await delImg(item["file_path"]); //删除本地图片
            });
            return true;
        }
        return false;
    },
};
